<?php
namespace isoftstone\ueditor\Listener;

use Flarum\Event\ConfigureFormatter;
use Illuminate\Contracts\Events\Dispatcher;

class AddContentFormat
{
    protected $whitList = [
        'a'=>      ['target', 'href', 'title', 'class', 'style'],
        'abbr'=>   ['title', 'class', 'style'],
        'address'=> ['class', 'style'],
        'area'=>   ['shape', 'coords', 'href', 'alt'],
        'article'=> [],
        'aside'=>  [],
        'audio'=>  ['autoplay', 'controls', 'loop', 'preload', 'src', 'class', 'style'],
        'b'=>      ['class', 'style'],
        'bdi'=>    ['dir'],
        'bdo'=>    ['dir'],
        'big'=>    [],
        'blockquote'=> ['cite', 'class', 'style'],
        'br'=>     [],
        'caption'=> ['class', 'style'],
        'center'=> [],
        'cite'=>   [],
        'code'=>   ['class', 'style'],
        'col'=>    ['align', 'valign', 'span', 'width', 'class', 'style'],
        'colgroup'=> ['align', 'valign', 'span', 'width', 'class', 'style'],
        'dd'=>     ['class', 'style'],
        'del'=>    ['datetime'],
        'details'=> ['open'],
        'div'=>    ['class', 'style'],
        'dl'=>     ['class', 'style'],
        'dt'=>     ['class', 'style'],
        'em'=>     ['class', 'style'],
        'font'=>   ['color', 'size', 'face'],
        'footer'=> [],
        'h1'=>     ['class', 'style'],
        'h2'=>     ['class', 'style'],
        'h3'=>     ['class', 'style'],
        'h4'=>     ['class', 'style'],
        'h5'=>     ['class', 'style'],
        'h6'=>     ['class', 'style'],
        'header'=> [],
        'hr'=>     [],
        'i'=>      ['class', 'style'],
        'img'=>    ['src', 'alt', 'title', 'width', 'height', 'id', 'loadingclass', 'class', 'data-latex'],
        'ins'=>    ['datetime'],
        'li'=>     ['class', 'style'],
        'mark'=>   [],
        'nav'=>    [],
        'ol'=>     ['class', 'style'],
        'p'=>      ['class', 'style'],
        'pre'=>    ['class', 'style'],
        's'=>      [],
        'section'=>[],
        'small'=>  [],
        'span'=>   ['class', 'style'],
        'sub'=>    ['class', 'style'],
        'sup'=>    ['class', 'style'],
        'strong'=> ['class', 'style'],
        'table'=>  ['width', 'border', 'align', 'valign', 'class', 'style'],
        'tbody'=>  ['align', 'valign', 'class', 'style'],
        'td'=>     ['width', 'rowspan', 'colspan', 'align', 'valign', 'class', 'style'],
        'tfoot'=>  ['align', 'valign', 'class', 'style'],
        'th'=>     ['width', 'rowspan', 'colspan', 'align', 'valign', 'class', 'style'],
        'thead'=>  ['align', 'valign', 'class', 'style'],
        'tr'=>     ['rowspan', 'align', 'valign', 'class', 'style'],
        'tt'=>     [],
        'u'=>      [],
        'ul'=>     ['class', 'style'],
        'video'=>  ['autoplay', 'controls', 'loop', 'preload', 'src', 'height', 'width', 'class', 'style']
        ];

    /**
     * @param Dispatcher $events
     */
    public function subscribe(Dispatcher $events)
    {
        $events->listen(ConfigureFormatter::class, [$this, 'configure']);
    }

    /**
     * @param ConfigureFormatter $event
     */
    public function configure(ConfigureFormatter $event)
    {
        $event->configurator->HTMLComments;
        $event->configurator->HTMLEntities;
        foreach($this->whitList as $key => $value){
            $event->configurator->HTMLElements->allowElement($key);
            if(count($value)>0){
                foreach($value as $item){
                    $event->configurator->HTMLElements->allowUnsafeAttribute($key, $item);
                }
            }
        }
    }
}
